package com.wxlsoft.excel.convert.listeners;

import java.util.Objects;

import javax.swing.JProgressBar;

import com.alibaba.excel.context.AnalysisContext;
import com.wxlsoft.excel.convert.model.BasePackageRecord;
import com.wxlsoft.excel.convert.model.ServiceRecordTypeEnum;
import com.wxlsoft.excel.convert.model.SheetDataConvertCollector;
import com.wxlsoft.excel.core.AbstractSheetListener;

import lombok.NonNull;

/**
 * 抽象报文头
 * @author wangxiaoliang
 */
public abstract class AbstractPackageListener extends AbstractSheetListener<BasePackageRecord> {

	protected final SheetDataConvertCollector collector;
	
	private ServiceRecordTypeEnum recordType = ServiceRecordTypeEnum.NONE;
	
	public AbstractPackageListener(String sheetName, JProgressBar progress,
			@NonNull final SheetDataConvertCollector collector) {
		super(sheetName, progress);
		this.collector = collector;
	}
	
	@SuppressWarnings("deprecation")
	public void isInputCheck(BasePackageRecord data, AnalysisContext context) {
		String record = data.getEnNamel();
		if (Objects.isNull(record)) return;
		
		if ("交易码".equals(record) && context.getCurrentRowNum() == 0) {
			this.recordType = ServiceRecordTypeEnum.HEAD;
		} else if ("输入".equals(record)) {
			this.recordType = ServiceRecordTypeEnum.INPUT;
		} else if ("输出".equals(record)) {
			this.recordType = ServiceRecordTypeEnum.OUTPUT;
		}
	}
	
	@Override
    public void invoke(BasePackageRecord data, AnalysisContext context) {
		isInputCheck(data, context);
		if (!isSkip(data, context)) {
			if (ServiceRecordTypeEnum.HEAD == recordType) {
				serviceHead(data, context);
			} else if (ServiceRecordTypeEnum.INPUT == recordType) {
				serviceBodyInput(data, context);
			} else if (ServiceRecordTypeEnum.OUTPUT == recordType){
				serviceBodyOutput(data, context);
			}
		}
    }

	/**
	 * 路过当前行
	 * @param data
	 * @param context
	 * @return
	 */
	protected boolean isSkip(BasePackageRecord data, AnalysisContext context) {
		return false;
	}

	/**
	 * 头数据
	 * @param data
	 * @param context
	 */
	protected void serviceHead(BasePackageRecord data, AnalysisContext context) {
		
	}

	/**
	 * 报文输入
	 * @param data
	 * @param context
	 */
	protected abstract void serviceBodyInput(BasePackageRecord data, AnalysisContext context);
	
	/**
	 * 报文输出
	 * @param data
	 * @param context
	 */
	protected abstract void serviceBodyOutput(BasePackageRecord data, AnalysisContext context);

}
